package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.SalePigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.SalePig;
import cn.edu.scau.cmi.hombio.domain.Task;

import cn.edu.scau.cmi.hombio.service.SaleService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Sale entities
 * 
 */

@Controller("SaleRestController")

public class SaleRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages SalePig entities
	 * 
	 */
	@Autowired
	private SalePigDAO salePigDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Sale entities
	 * 
	 */
	@Autowired
	private SaleService saleService;

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@RequestMapping(value = "/Sale/{sale_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveSaleEmployee(@PathVariable Integer sale_id, @RequestBody Employee employee) {
		saleService.saveSaleEmployee(sale_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Delete an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/salePigs/{salepig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSaleSalePigs(@PathVariable Integer sale_id, @PathVariable Integer related_salepigs_id) {
		saleService.deleteSaleSalePigs(sale_id, related_salepigs_id);
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newSaleTask(@PathVariable Integer sale_id, @RequestBody Task task) {
		saleService.saveSaleTask(sale_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Delete an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSale(@PathVariable Integer sale_id) {
		Sale sale = saleDAO.findSaleByPrimaryKey(sale_id);
		saleService.deleteSale(sale);
	}

	/**
	* View an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/salePigs/{salepig_id}", method = RequestMethod.GET)
	@ResponseBody
	public SalePig loadSaleSalePigs(@PathVariable Integer sale_id, @PathVariable Integer related_salepigs_id) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(related_salepigs_id, -1, -1);

		return salepig;
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadSaleTask(@PathVariable Integer sale_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newSaleEmployee(@PathVariable Integer sale_id, @RequestBody Employee employee) {
		saleService.saveSaleEmployee(sale_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Save an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/salePigs", method = RequestMethod.PUT)
	@ResponseBody
	public SalePig saveSaleSalePigs(@PathVariable Integer sale_id, @RequestBody SalePig salepigs) {
		saleService.saveSaleSalePigs(sale_id, salepigs);
		return salePigDAO.findSalePigByPrimaryKey(salepigs.getId());
	}

	/**
	* Show all Sale entities
	* 
	*/
	@RequestMapping(value = "/Sale", method = RequestMethod.GET)
	@ResponseBody
	public List<Sale> listSales() {
		return new java.util.ArrayList<Sale>(saleService.loadSales());
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveSaleTask(@PathVariable Integer sale_id, @RequestBody Task task) {
		saleService.saveSaleTask(sale_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Save an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/Sale", method = RequestMethod.PUT)
	@ResponseBody
	public Sale saveSale(@RequestBody Sale sale) {
		saleService.saveSale(sale);
		return saleDAO.findSaleByPrimaryKey(sale.getId());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSaleTask(@PathVariable Integer sale_id, @PathVariable Integer related_task_id) {
		saleService.deleteSaleTask(sale_id, related_task_id);
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSaleEmployee(@PathVariable Integer sale_id, @PathVariable Integer related_employee_id) {
		saleService.deleteSaleEmployee(sale_id, related_employee_id);
	}

	/**
	* Get Employee entity by Sale
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getSaleEmployee(@PathVariable Integer sale_id) {
		return saleDAO.findSaleByPrimaryKey(sale_id).getEmployee();
	}

	/**
	* Select an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}", method = RequestMethod.GET)
	@ResponseBody
	public Sale loadSale(@PathVariable Integer sale_id) {
		return saleDAO.findSaleByPrimaryKey(sale_id);
	}

	/**
	* Create a new Sale entity
	* 
	*/
	@RequestMapping(value = "/Sale", method = RequestMethod.POST)
	@ResponseBody
	public Sale newSale(@RequestBody Sale sale) {
		saleService.saveSale(sale);
		return saleDAO.findSaleByPrimaryKey(sale.getId());
	}

	/**
	* Get Task entity by Sale
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getSaleTask(@PathVariable Integer sale_id) {
		return saleDAO.findSaleByPrimaryKey(sale_id).getTask();
	}

	/**
	* Create a new SalePig entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/salePigs", method = RequestMethod.POST)
	@ResponseBody
	public SalePig newSaleSalePigs(@PathVariable Integer sale_id, @RequestBody SalePig salepig) {
		saleService.saveSaleSalePigs(sale_id, salepig);
		return salePigDAO.findSalePigByPrimaryKey(salepig.getId());
	}

	/**
	* Show all SalePig entities by Sale
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/salePigs", method = RequestMethod.GET)
	@ResponseBody
	public List<SalePig> getSaleSalePigs(@PathVariable Integer sale_id) {
		return new java.util.ArrayList<SalePig>(saleDAO.findSaleByPrimaryKey(sale_id).getSalePigs());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Sale/{sale_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadSaleEmployee(@PathVariable Integer sale_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}
}